package action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts.action.*;

import bean.IpApplyStatusListForm;

import dao.TIpApplyDao;
import entity.TIpApply;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class IpApplyStatusListAction extends Action {

    public ActionForward execute(ActionMapping map, ActionForm frm,
            HttpServletRequest request, HttpServletResponse response) {

        List<TIpApply> allApply = TIpApplyDao.getAllApply();

        // TODO null のときの処理を変更
        if (allApply != null) {
            List<IpApplyStatusListForm> statusList = new ArrayList<IpApplyStatusListForm>();
            for (TIpApply apply : allApply) {
                String userId = (String) request.getSession().getAttribute("userId");
                if (apply.getUserId() != userId) {
                    continue;
                }
                IpApplyStatusListForm form = new IpApplyStatusListForm();
                form.setApplyId(apply.getApplyId());
                // 申請状況
                String status = null;
                Integer statusInt = apply.getStatus();
                switch (statusInt) {
                case 0:
                    status = "申請中";
                    break;
                case 1:
                    status = "IP割り当て済み";
                    break;
                case 2:
                    status = "申請却下";
                    break;
                default:
                    status = "申請中";
                    break;
                }
                form.setStatus(status);
                form.setPlace(apply.getPlace());
                form.setMacAddress(apply.getMacAddress());
                form.setIpAddress(apply.getIpAddress());
                statusList.add(form);
            }

            request.setAttribute("statusList", statusList);
            return map.findForward("success");
        }
        else {
            return map.findForward("error");
        }

    }
}
